package com.chestnut.contentcore.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.db.domain.BaseEntity;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.contentcore.domain.CmsCarousel;
import com.chestnut.contentcore.domain.CmsCertificationsStandards;
import com.chestnut.contentcore.service.ICmsCertificationsStandardsService;
import com.chestnut.system.security.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chestnut.common.security.web.BaseRestController;

import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * 认证和标准Controller
 *
 * @author chestnut
 * @date 2025-04-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/standards")
public class CmsCertificationsStandardsController extends BaseRestController {

    @Autowired
    private ICmsCertificationsStandardsService cmsCertificationsStandardsService;

    /**
     * 查询认证和标准列表
     */
//    @Priv
    @GetMapping("/list")
    public R<?> list(CmsCertificationsStandards cmsCertificationsStandards)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsCertificationsStandards> wrapper = this.cmsCertificationsStandardsService.lambdaQuery();
                    if (cmsCertificationsStandards.getName() != null) {
                        wrapper.like(CmsCertificationsStandards::getName,cmsCertificationsStandards.getName());
                    }
        if (cmsCertificationsStandards.getLanguage() != null) {
            wrapper.like(CmsCertificationsStandards::getLanguage,cmsCertificationsStandards.getLanguage());
        }
        wrapper.orderByAsc(CmsCertificationsStandards::getSort);
        Page<CmsCertificationsStandards> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }

    /**
     * 获取认证和标准详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:standards:query')")
    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsCertificationsStandardsService.getById(id));
    }

    /**
     * 新增认证和标准
     */
//    @Priv
    @Log(title = "认证和标准", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody CmsCertificationsStandards cmsCertificationsStandards)
    {

        cmsCertificationsStandards.setCreateBy(StpAdminUtil.getLoginUser().getUsername());
        cmsCertificationsStandards.setCreateTime(LocalDateTime.now());
        this.cmsCertificationsStandardsService.save(cmsCertificationsStandards);
        return R.ok();
    }

    /**
     * 修改认证和标准
     */
//    @Priv
    @Log(title = "认证和标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsCertificationsStandards cmsCertificationsStandards)
    {
        this.cmsCertificationsStandardsService.updateById(cmsCertificationsStandards);
        return R.ok();
    }

    /**
     * 删除认证和标准
     */
//    @Priv
    @Log(title = "认证和标准", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsCertificationsStandardsService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
